﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;

namespace MapService
{
    [AspNetCompatibilityRequirements(RequirementsMode
          = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        static string connectionString = @"Data Source=(Local)\SqlExpress;" +
              @"AttachDbFilename=|DataDirectory|GoogleMap.mdf;" +
              "Integrated Security=true;" +
              "User Instance=true";
        DuLieuDataContext dc = new DuLieuDataContext(connectionString);
        public TAIKHOAN getTK(string user_name, string pw)
        {
            try
            {
                TAIKHOAN sv = dc.TAIKHOANs.Single(p => p.UserName == user_name & p.PassWord == pw);
                return sv;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool SaveTK(TAIKHOAN sv)
        {
            try
            {
                dc.TAIKHOANs.InsertOnSubmit(sv);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool updateTK(TAIKHOAN Sv)
        {
            try
            {
                TAIKHOAN sv = dc.TAIKHOANs.Single(p => p.ID == Sv.ID);
                sv.UserName = Sv.UserName;
                sv.PassWord = Sv.PassWord;
                sv.Email = Sv.Email;
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool SaveDiaDiem(DIADIEM _diadiem)
        {
            try
            {
                dc.DIADIEMs.InsertOnSubmit(_diadiem);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        /// <summary>
        /// Cập Nhật Một Địa Điểm
        /// </summary>
        /// <param name="_diadiem">Dịa Điểm</param>
        /// <returns> True: Thành công|| False: Thất Bại</returns>
        public bool Update(DIADIEM _diadiem)
        {
            try
            {
                DIADIEM dd = dc.DIADIEMs.Single(p => p.ID == _diadiem.ID);
                dd.TenDiaDiem = _diadiem.TenDiaDiem;
                dd.ID_TaiKhoan = _diadiem.ID_TaiKhoan;
                dd.ID_DichVu = _diadiem.ID_DichVu;
                dd.TungDo = _diadiem.TungDo;
                dd.ViDo = _diadiem.ViDo;
                dd.GhiChu = _diadiem.GhiChu;
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool deleteDiaDiem(int ID)
        {
            try
            {
                DIADIEM sv = dc.DIADIEMs.Single(p => p.ID == ID);
                dc.DIADIEMs.DeleteOnSubmit(sv);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public List<DIADIEM> getDSDiaDiemFromDichVu(int id)
        {
            var rs = from p in dc.DIADIEMs
                     where p.DICHVU.ID == id
                     select p;
            List<DIADIEM> kq = rs.ToList();
            return kq;
        }
        public List<DIADIEM> getDSDiaDiemFromUSer(int id_user)
        {
            var rs = from p in dc.DIADIEMs
                     where p.TAIKHOAN.ID == id_user
                     select p;
            return rs.ToList();
        }
        public DIADIEM getDiaDiemFromID(int id_user)
        {
            DIADIEM a = dc.DIADIEMs.Single(p => p.ID == id_user) as DIADIEM;
            return a;
        }
        public List<DICHVU> getAllDV()
        {
            List<DICHVU> a = dc.DICHVUs.ToList();
            return a;
        }
        public List<DIADIEM> getDSDiaDiemFromUSerAndDV(int id_user, int id_dv)
        {
            List<DIADIEM> kq = new List<DIADIEM>();
            var a = dc.DIADIEMs.Where(p => ((p.ID_TaiKhoan == id_user) & (id_dv == p.ID_DichVu)));
            kq = a.ToList();
            return kq;
        }
        public string AddNewContact(int id_user, string contact_name)
        {
            try
            {
                TAIKHOAN myTK = dc.TAIKHOANs.Single(p => p.ID == id_user);
                TAIKHOAN myContack = dc.TAIKHOANs.SingleOrDefault(p => p.UserName.Contains(contact_name.Trim()));
                if (myContack == null)
                    return "Khong Co Nguoi Dung Ten" + contact_name;
                else
                {
                    Contact kq = new Contact();
                    kq.ID_Contact = myContack.ID;
                    myTK.Contacts.Add(kq); dc.SubmitChanges();
                    return "Add Thành Công";
                }
            }
            catch (Exception ex)
            {
                return "Khong Co Nguoi Dung Ten" + contact_name;
            }
        }
        public List<TAIKHOAN> getContacts(int id_user)
        {
            List<TAIKHOAN> kq = new List<TAIKHOAN>();
            var tk = from p in dc.Contacts
                     where (p.ID_TK == id_user)
                     select p.TAIKHOAN1;
            kq = tk.ToList();
            return kq;
        }
        public bool ShareDiaDiem(int id_user, int id_diadiem)
        {
            try
            {
                DIADIEM tam = dc.DIADIEMs.SingleOrDefault(p => p.ID == id_diadiem);
                DIADIEM dd = new DIADIEM();
                dd.TenDiaDiem = tam.TenDiaDiem;
                dd.ID_TaiKhoan = id_user;
                dd.ID_DichVu = tam.ID_DichVu; dd.ViDo = tam.ViDo;
                dd.TungDo = tam.TungDo;
                dd.GhiChu = tam.GhiChu;
                dc.DIADIEMs.InsertOnSubmit(dd);
                dc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public DIADIEM SearchNearBy(int id_diadiem)
        {
            DIADIEM dd = dc.DIADIEMs.SingleOrDefault(p=>p.ID==id_diadiem);
            var tam = from p in dc.DIADIEMs
                      where (p.ID == id_diadiem)
                      select p.DICHVU.DIADIEMs;
            var c = tam.FirstOrDefault();
            List<DIADIEM> kq =   c.ToList();
            kq.Remove(dd);
            DIADIEM res = kq[0];
            double kc=khoangcanh(res,dd);
            foreach (DIADIEM  a  in kq)
            {
                if ( khoangcanh(a, dd)!=0 & khoangcanh(a, dd) < kc)
                {
                    kc = khoangcanh(a, dd);
                    res = a;
                }
             
            }
            return res;
        }
        private double khoangcanh(DIADIEM dd1,DIADIEM dd2)
        {
            double a = (double)(dd1.ViDo - dd2.ViDo);
            double b = (double)(dd1.TungDo - dd2.TungDo);
            return Math.Sqrt(Math.Pow(a,2)+Math.Pow(b,2)); 
        }

    }
   
}
